﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Transactions;
using System.Web.Mvc;
using System.Web.UI.WebControls;
using ZBJF.Warranty.BLL;
using ZBJF.Warranty.Common.Common;
using ZBJF.Warranty.Common.Enums.System;
using ZBJF.Warranty.Model;
using ZBJF.Warranty.Model.ViewModel.Sellers;
using ZBJF.Warranty.WebProvider;


namespace ZBJF.Warranty.AdminSite.Controllers
{
    public class SellersOrganizationController : BaseController
    {
        //
        // GET: /SellersOrganizationManage/
        public ActionResult Index()
        {
            return View();
        }
        #region 保存销售机构信息   ActionResult UpdateSellerOrganizationForm(SellersOrganization model)
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult UpdateSellerOrganizationForm(SellersOrganization model)
        {
            var responseResult = new ResponseResult
            {
                OpreateType = model.ID > 0 ? OpreateType.Update : OpreateType.Add
            };
            var serv = ServiceFactory.SellersOrganizationServiceInstance;
            if (model.ID > 0)
            {
                using (TransactionScope sc = new TransactionScope())
                {
                    try
                    {
                        if (model.ID == model.ParentOrganizationId)
                        {
                            responseResult.Message = "不能将自己作为自己的父类";
                        }
                        else
                        {
                            var result = ServiceFactory.SellersOrganizationServiceInstance.IsExist(model.OrganizationName, model.ID);
                            if (result)
                            {
                                responseResult.Message = "页面名称已经存在了";
                            }
                            else
                            {
                                var list = serv.LoadByID(model.ID);
                                list.LastModifyBy = CurrentUser.UserAccount;
                                list.LastModifyTime = DateTime.Now;
                                list.Remark = model.Remark;
                                list.IsAvailable = model.IsAvailable;
                                list.OrganizationName = model.OrganizationName;
                                list.ParentOrganizationId = model.ParentOrganizationId;
                                list.Sort = model.Sort;
                                list.SellerType = model.SellerType;
                                responseResult.Success = serv.UpdateEntities(list);
                                sc.Complete();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        sc.Dispose();
                    }
                }
            }
            else
            {
                if (serv.IsExist(model.OrganizationName))
                {
                    responseResult.Message = "销售机构名称已存在";
                }
                else
                {
                    model.CreateTime = DateTime.Now;
                    model.CreateBy = CurrentUser.UserAccount;
                    model.LastModifyTime = DateTime.Now;
                    model.LastModifyBy = CurrentUser.UserAccount;
                    var entity = serv.AddEntities(model);
                    responseResult.Success = entity.ID > 0;
                }
            }
            TempData["ResponseResult"] = responseResult;
            return RedirectToAction("Index");
        }
        #endregion
        /// <summary>
        /// 获取页面信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public JsonResult GetSellersOrganizationInfo(int id = 0)
        {
            var result = ServiceFactory.SellersOrganizationServiceInstance.LoadEntities(m => m.ID == id).Select(o => new
            {
                o.ID,
                o.IsAvailable,
                o.OrganizationName,
                o.ParentOrganizationId,
                o.Sort,
                o.SellerType
            }).FirstOrDefault();
            if (result == null)
            {
                return Json(new { success = false, message = "页面不存在" });
            }
            else
            {
                return Json(new { success = true, result });
            }
        }
        #region 删除销售机构  ActionResult DeleteSellersOrganization(int id = 0)
        public ActionResult DeleteSellersOrganization(int id = 0)
        {
            bool status = false;
            var entity = ServiceFactory.SellersOrganizationServiceInstance.LoadEntities(p => p.ID == id).FirstOrDefault();
            if (entity != null)
                if (ServiceFactory.SellersOrganizationServiceInstance.DeleteEntities(entity))
                {
                    status = true;
                }
            string message = status ? "删除成功！" : "删除失败！";
            return Json(new { success = status, message = message });
        }
        #endregion
        /// <summary>
        /// 获取销售机构列表
        /// </summary>
        /// <param name="listPage"></param>
        /// <param name="pid"></param>
        /// <param name="layer"></param>
        /// <returns></returns>

        public ActionResult ShowMenuList(List<SellersOrganization> listPage, int pid = 0, int layer = 0)
        {
            ViewBag.Pid = pid;
            ViewBag.Layer = layer;
            return View(listPage);
        }
        #region 获取机构列表  ActionResult ShowMenuSelectList(List<SellersOrganization> listPage, int pid = 0, int layer = 0)
        public ActionResult ShowMenuSelectList(List<SellersOrganization> listPage, int pid = 0, int layer = 0)
        {
            ViewBag.Pid = pid;
            ViewBag.Layer = layer;
            return View(listPage);
        }
        #endregion
    }      
}